// Home sql
const { connection } = require('../db/mariadb');
//查询获取首页列表数据
const getQueryHomeData = async (id,title,url) => {
    let sql = `SELECT * FROM home_table WHERE 1=1`, result;
    if (id) {
        sql += ` AND id = '${id}'`
    }
    if (title) {
        sql += ` AND title='${title}'`
    }
    if (url) {
        sql += ` AND url='${url}'`
    }
    sql += ' ORDER BY id asc,title asc;'
    result = await connection(sql);
    return result;
};
//home表添加数据
const createDataToHomeTable = async (data) => {
    let sql = `INSERT INTO home_table (title,url,des) VALUES (
        '${data.title}',
        '${data.url}',
        '${data.des}');`,
        result;
    result = await connection(sql);
    return result;
};

//更新数据表中已有的数据
const updateDataToHomeTable = async (data) => {
    let sql = `UPDATE home_table SET 
    title='${data.title}' , 
    url='${data.url}' , 
    des='${data.des}' 
    WHERE id='${data.id}';`,
        result;
    result = await connection(sql);
    return result;
};

//删除某一条数据
const deleteDataFromHomeTable = async (id) => {
    let sql = `DELETE FROM home_table WHERE id='${id}';`,result;
    result = await connection(sql);
    return result;
};


module.exports = {
    getQueryHomeData,
    createDataToHomeTable,
    updateDataToHomeTable,
    deleteDataFromHomeTable
}